package com.ywjh.inter.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ywjh.inter.entity.BGluRangeEntity;
import com.ywjh.inter.service.SettingService;
import com.ywjh.inter.utils.StrUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class Setting extends HttpServlet {

	/**
	 *
	 */
	private static final long serialVersionUID = -1059745374810087112L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html; charset=utf-8");
		int r = 0;
		String e = "";

		JSONObject postData = StrUtil.getPostJSON(req);
		String act = postData.getString("act");
		JSONObject data = postData.getJSONObject("data");
		JSONObject json = new JSONObject();
		switch (act) {
		case "set_bglu_range":
			int userId = data.getInt("user_id");
			String[] planId = data.getString("plan_array").split(",");
			String[] range = data.getString("range_array").split(",");
			try {
				List<BGluRangeEntity> list = new LinkedList<>();
				for (int i = 0; i < planId.length; i++) {
					BGluRangeEntity bGluRangeEntity = new BGluRangeEntity();
					bGluRangeEntity.setUser_id(userId);
					bGluRangeEntity.setWarning_range(range[i]);
					int plan_id = Integer.parseInt(planId[i]);
					bGluRangeEntity.setPlan_id(plan_id);
					bGluRangeEntity.setStatus(1);
					list.add(bGluRangeEntity);
				}
				boolean b = SettingService.getInstance().setBgluRange(list, userId);
				if (b) {
					r = 1;
				} else {
					e = "服务器故障";
				}
			} catch (Exception e2) {
				e = "数据异常";
			}
			break;
		case "get_bglu_range":
			userId = data.getInt("user_id");
			Map<Integer, BGluRangeEntity> map = SettingService.getInstance().getBgluRange(userId);
			System.out.println("get_bglu_range map==="+map.size());
			JSONArray array = new JSONArray();
			for (Integer key : map.keySet()) {
				BGluRangeEntity bGluRangeEntity = map.get(key);
				float low = 0, hi = 0;
				try {
					range = bGluRangeEntity.getWarning_range().split(";");
					low = Float.parseFloat(range[0]);
					hi = Float.parseFloat(range[1]);
				} catch (Exception e1) {
					System.out.println(e1.getMessage());
				}
				JSONObject obj = new JSONObject();
				obj.put("plan_id", key);
				obj.put("user_id", bGluRangeEntity.getUser_id());
				obj.put("low", low);
				obj.put("hi", hi);
				array.add(obj);
			}

			r = 1;
			json.put("d", array);
			break;
		}
		json.put("r", r);
		json.put("e", e);
		PrintWriter pw = resp.getWriter();
		pw.write(json.toString());
		pw.flush();
		pw.close();
	}

}
